package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import model.CbbModel;
import model.dao.AccountDAO;
import modifycomponent.JImageViewer;
import controller.TokenGetterController;

public class TokenGetter extends JFrame {
	private static final long serialVersionUID = 0x27AB8CL;

	private JButton btn_getToken;
	private JComboBox<String> cb_user;
	private JProgressBar pb_countDown;
	private JPanel pn_container;
	private JImageViewer pn_icon;
	private JTextField txt_pin;
	private JTextField txt_token;

	public TokenGetter() {
		init();
	}

	private void init() {
		newComponents();
		setPnIconLayout();
		setPnContainerLayout();
		setMainLayout();

		controller();
	}

	private void newComponents() {
		pn_icon = new JImageViewer();
		pn_container = new JPanel();
		cb_user = new JComboBox<>();
		txt_token = new JTextField();
		txt_pin = new JTextField();
		pb_countDown = new JProgressBar(SwingConstants.VERTICAL, 0, 60);
		btn_getToken = new JButton();
	}

	private void setPnIconLayout() {
		pn_icon.setBorder(BorderFactory.createTitledBorder(""));
		pn_icon.setPreferredSize(new Dimension(100, 110));
		pn_icon.setLayout(null);
	}

	private void setPnContainerLayout() {
		pn_container.setBorder(BorderFactory.createTitledBorder(""));
		pn_container.setPreferredSize(new Dimension(240, 100));
		pn_container.setLayout(null);

		cb_user.setFont(new Font("Courier New", 1, 13));
		cb_user.setModel(CbbModel.getUsernames(new AccountDAO()
				.getAllUserName()));
		pn_container.add(cb_user);
		cb_user.setBounds(10, 10, 90, 30);

		txt_token.setEditable(false);
		txt_token.setFont(new Font("Courier New", 1, 24));
		txt_token.setHorizontalAlignment(JTextField.CENTER);
		txt_token.setText("696 969");
		pn_container.add(txt_token);
		txt_token.setBounds(30, 50, 200, 40);

		txt_pin.setEditable(false);
		txt_pin.setFont(new Font("Courier New", 1, 16));
		txt_pin.setHorizontalAlignment(JTextField.CENTER);
		txt_pin.setText("35210964");
		pn_container.add(txt_pin);
		txt_pin.setBounds(110, 10, 120, 30);

		pb_countDown.setBackground(new Color(255, 255, 255));
		pb_countDown.setMaximum(60);
		pb_countDown.setOrientation(1);
		pb_countDown.setValue(60);
		pb_countDown.setString("");
		pb_countDown.setStringPainted(true);
		pn_container.add(pb_countDown);
		pb_countDown.setBounds(10, 50, 20, 41);
	}

	private void setMainLayout() {
		getContentPane().setLayout(null);

		getContentPane().add(pn_icon);
		pn_icon.setBounds(10, 10, 100, 100);
		pn_icon.setUrl("src/icons/token_icon.png");
		pn_icon.repaint();
		
		getContentPane().add(pn_container);
		pn_container.setBounds(110, 10, 240, 100);

		btn_getToken.setFont(new Font("Courier New", 0, 24));
		btn_getToken.setText("Get New Token");
		getContentPane().add(btn_getToken);
		btn_getToken.setBounds(10, 120, 340, 40);

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setIconImage(new ImageIcon("src/icons/small_token_icon.png").getImage());
		setTitle("HQT - Token");
		setResizable(false);
		setSize(370, 200);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	private void controller() {
		new TokenGetterController(this);
	}

	public JButton getBtn_applyChange() {
		return btn_getToken;
	}

	public JComboBox<String> getCb_user() {
		return cb_user;
	}

	public JProgressBar getPb_countDown() {
		return pb_countDown;
	}

	public JPanel getPn_container() {
		return pn_container;
	}

	public JImageViewer getPn_icon() {
		return pn_icon;
	}

	public JTextField getTxt_pin() {
		return txt_pin;
	}

	public JTextField getTxt_token() {
		return txt_token;
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			new TokenGetter();
		} catch (Exception e) {
		}
	}
}
